namespace Project.MVC.PhanQuyen.MsSQLDAL
{
    using System;
    using System.Linq;
    using IDAL;
    using Model;

	public class PQNhomCongViecRepository : IPQNhomCongViec
	{
		LinqPhanQuyenDataContext dataContext;
        
		public PQNhomCongViecRepository()
		{
             dataContext = new LinqPhanQuyenDataContext();
		}
		
        #region IPQNhomCongViec Members
        public PQNhomCongViecInfo GetByKey( int id )
		{
            try
			{
                var info = (from record in dataContext.PQ_NhomCongViecs
                           where record.id == id
                           select General.GetInfo(record)
                           ).SingleOrDefault();
                return info;								
			}
			catch(Exception)
			{
				throw;
			}
		}
		#endregion
		
        #region IDALBase<PQNhomCongViecInfo> Members
        public IQueryable<PQNhomCongViecInfo> GetList()
		{
            try
			{
                var list = from record in dataContext.PQ_NhomCongViecs
                           select General.GetInfo(record);
                return list;								
			}
			catch(Exception)
			{
				throw;
			}
		}
        
        public bool Insert(PQNhomCongViecInfo value)
        {
            if (value == null)
                return false;
            try
            {
                value.Id = getNewID();
                var info = General.GetInfo(value);

                dataContext.PQ_NhomCongViecs.InsertOnSubmit(info);

                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }
        
        public bool Update(PQNhomCongViecInfo value)
        {
            if (value == null)
                return false;
            try
            {
                var info = (from record in dataContext.PQ_NhomCongViecs
                           where record.id == value.Id && 1==1
                           select record)
                           .SingleOrDefault();                

                if (info != null)
                {
                    info.tenNhom = value.TenNhom;
                    info.ghiChu = value.GhiChu;
                    info.userName = value.UserName;
                    info.userFullName = value.UserFullName;
                    info.timeInput = value.TimeInput;
                    //info.rowVersion = value.RowVersion;
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                throw;
            }
        }
        
        public bool Delete(PQNhomCongViecInfo value)
        {
            if (value == null) 
                return false;
            try
            {
                var info = (from record in dataContext.PQ_NhomCongViecs
                           where record.id == value.Id && 1==1
                           select record)
                           .SingleOrDefault();

                if (info != null)
                {
                    dataContext.PQ_NhomCongViecs.DeleteOnSubmit(info);
                    return true;
                }

                return false;
            }
            catch (Exception)
            {
                throw;
            }
        }
        
        public int Count()
        {
            try
            {
                var value = (from record in dataContext.PQ_NhomCongViecs
                            select record)
                            .Count();
                return value;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public void Commit()
        {
            dataContext.SubmitChanges();
        }
        #endregion
        
        #region Private Methods
        protected int getNewID()
        {
            try
            {
                int? id = (from record in dataContext.PQ_NhomCongViecs
                          select (int?)record.id)
                          .Max();
                if (id == null)
                    return 1;
                else
                    return ((int)id) + 1;
            }
            catch (Exception)
            {
                return 1;
            }
        }
        #endregion
	}
}